﻿using System;


namespace Development.Tools
{

    public delegate void WatcherEvent(int time);
    public delegate void WatcherWeekEvent(DayOfWeek day);
    /// <summary>
    /// 定义个计时器
    /// </summary>
    /// <param name="time"></param>
    public class Watcher
    {
        public event WatcherEvent OnYearChange;
        public event WatcherEvent OnMonthChange;
        public event WatcherEvent OnDayChange;
        public event WatcherWeekEvent OnWeekDayChange;
        public event WatcherEvent OnHourChange;
        public event WatcherEvent OnMinutesChange;
        public event WatcherEvent OnSecondsChange;
        private DateTime _lastTime = DateTime.Now;

        private System.Timers.Timer _timer;

        public void StartWatch()
        {
            if(_timer != null)
            {
                StopWatch();
            }
            _lastTime = DateTime.Now;
            _timer = new System.Timers.Timer(1000);
            _timer.Elapsed += _timer_Elapsed;
            _timer.Start();
            Log.WriteLog(ELogLevel.LOG_INFO, "Watcher start now:" + _lastTime.ToString());
        }

        private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DateTime now = DateTime.Now;
            if(OnSecondsChange != null)
            {
                OnSecondsChange(now.Second);
            }
            if (now.Minute != _lastTime.Minute)
            {
                if (OnMinutesChange != null)
                {
                    OnMinutesChange(now.Minute);
                }
                if(now.Hour != _lastTime.Hour)
                {
                    if (OnHourChange != null)
                    {
                        OnHourChange(now.Hour);
                    }
                    if(now.Day != _lastTime.Day)
                    {
                        if (OnDayChange != null)
                        {
                            OnDayChange(now.Day);
                        }
                        if (OnWeekDayChange != null)
                        {
                            OnWeekDayChange(now.DayOfWeek);
                        }
                        if(now.Month != _lastTime.Month)
                        {
                            if (OnMonthChange != null)
                            {
                                OnMonthChange(now.Month);
                            }
                            if(now.Year != _lastTime.Year)
                            {
                                if (OnYearChange != null)
                                {
                                    OnYearChange(now.Year);
                                }
                            }
                        }
                    }
                }
            }
            _lastTime = now;
        }

        public void StopWatch()
        {
            if (_timer != null)
            {
                _timer.Stop();
                _timer = null;
            }
        }
    }
}
